home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7020 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  100 lines

  1. Path: hermes.ucd.ie!usenet
  2. From: Luca Piccarreta <es-46@scolaire.ucd.ie>
  3. Newsgroups: comp.lang.c++
  4. Subject: Programming problem with Borland C++ 3.1
  5. Date: Wed, 21 Feb 1996 15:57:13 +0000
  6. Organization: University College Dublin
  7. Message-ID: <312B40D9.3E66@scolaire.ucd.ie>
  8. NNTP-Posting-Host: luca.ucd.ie
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Hi, I'm Luca Piccarreta, Italian Student temporarily in Ireland under
  15. ERASMUS program.
  16. I'm doing a program about speech Recognition and I've run into a problem
  17. that I'm not able to solve, because I simply don't understand it.
  18. I wrote this code:
  19.  
  20. template <class T>
  21. class vector{
  22. public:
  23.         virtual unsigned GetDim()const=0;
  24.         virtual T operator[](unsigned i)const=0;
  25.         virtual T& operator[](unsigned i)=0;
  26.  
  27.         int operator ==(const vector<T>& v);
  28.         virtual vector<T>& operator -=(const vector<T>& v);
  29.         virtual vector<T>& operator +=(const vector<T>& v);
  30.         virtual vector<T>& operator =(const vector<T>& v);
  31.         virtual vector<T>& Set(const vector<T>& v);
  32.  
  33.         virtual vector<T>& operator *=(const vector<T>& mul);
  34.         /* this is a product term by term, is not a real vector op
  35.         virtual vector<T>& operator *=(T mul);
  36.         virtual vector<T>& operator /=(T div);
  37.         virtual T operator *(const vector<T>& mul);
  38. };
  39.  
  40. template <class T>
  41. class vectorArr:public vector<T>{
  42.         int dim;
  43.         T* data;
  44. public:
  45.         vectorArr(int dim=0);
  46.         vectorArr(const vectorArr<T>& v);
  47.         ~vectorArr();
  48.         unsigned GetDim()const
  49.         {
  50.                 return dim;
  51.         }
  52.         void SetDim(unsigned _dim);
  53.         T operator[](unsigned i)const{
  54.                 PRECONDITION(i<dim);
  55.                 return data[i];
  56.         }
  57.         T& operator[](unsigned i){
  58.                 PRECONDITION(i<dim);
  59.                 return data[i];
  60.         }
  61. }
  62.  
  63. Now, I won't write all the code for these two classes, but I have a
  64. problem with the overloaded operator=. That is:
  65.  
  66.  
  67. template <class T>
  68. vector<T>& vector<T>::operator =(const vector<T>& v)
  69. {
  70.         PRECONDITION(GetDim()==v.GetDim());
  71.         for(int i=0;i<GetDim();i++)
  72.         {
  73.                 (*this)[i]=v[i];
  74.         }
  75.         return *this;
  76. }
  77. template <class T>
  78. vector<T>& vector<T>::Set(const vector<T>& v)
  79. {
  80.         PRECONDITION(GetDim()==v.GetDim());
  81.         for(int i=0;i<GetDim();i++)
  82.         {
  83.                 (*this)[i]=v[i];
  84.         }
  85.         return *this;
  86. }
  87.  
  88. these two functions should do exactly the same job, as far as I know,
  89. but they don't. why?
  90. If I try and use the operator= (between two elements of the derived
  91. class vectorArr) the program fails, and, by debugging, I discovered that
  92. the compiler generates different codes not for the functions, but at the
  93. point where the functions are called (they are not inlined).
  94. Can any guru or half-guru help me?
  95.  
  96. Thanks in advance (forgive my poor english).
  97.  
  98. Luca Piccarreta
  99. es-46@scolaire.ucd.ie
  100.